package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.domain.ProductInventory;
import com.example.mall.dto.ProductInventoryDto;
import com.example.mall.mapper.ProductInventoryMapper;
import com.example.mall.query.PageDTO;
import com.example.mall.query.ProductInventoryQuery;
import com.example.mall.service.IProductInventoryService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品库存表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class ProductInventoryServiceImpl extends ServiceImpl<ProductInventoryMapper, ProductInventory> implements IProductInventoryService {

    @Autowired
    private ProductInventoryMapper productInventoryMapper;

    @Override
    public PageDTO<ProductInventoryDto> queryPage(ProductInventoryQuery productInventoryQuery) {
        if (productInventoryQuery == null) {
            productInventoryQuery = new ProductInventoryQuery();
        }
        Page<ProductInventory> page = productInventoryQuery.toMpPageByDefaultCreateTimeAsc();
        Page<ProductInventory> cartPage = lambdaQuery()
                .like(productInventoryQuery.getInventoryId()!= null, ProductInventory::getInventoryId, productInventoryQuery.getInventoryId())
                .like(productInventoryQuery.getProductId()!= null, ProductInventory::getProductId, productInventoryQuery.getProductId())
                .like(productInventoryQuery.getSpecId()!= null, ProductInventory::getSpecId, productInventoryQuery.getSpecId())
                .like(productInventoryQuery.getStock()!= null, ProductInventory::getStock, productInventoryQuery.getStock())
                .like(productInventoryQuery.getReservedStock()!= null, ProductInventory::getReservedStock, productInventoryQuery.getReservedStock())
                .orderByDesc(ProductInventory::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, ProductInventoryDto.class);
    }

    @Override
    public ProductInventory queryProductInventoryById(Long inventoryId) {
        AssertUtil.isNotNull(inventoryId, "商品库存ID不能为空");
        return productInventoryMapper.selectById(inventoryId);
    }

    @Override
    public List<ProductInventory> queryProductInventoryList() {
        return this.list();
    }

    @Override
    public void insert(ProductInventoryDto productInventoryDto) {
        ProductInventory productInventory = new ProductInventory();
        CopyUtils.copyProperties(productInventoryDto, productInventory);
        productInventory.setInventoryId(CodeGenerateUtils.generateUniqueId());
        productInventory.setCreateDate(new Date());
        productInventory.setUpdateDate(new Date());
        productInventoryMapper.insert(productInventory);
    }

    @Override
    public void update(ProductInventoryDto productInventoryDto) {
        AssertUtil.isNotNull(productInventoryDto.getInventoryId(), "商品库存ID不能为空");
        ProductInventory productInventory = productInventoryMapper.selectById(productInventoryDto.getInventoryId());
        AssertUtil.isNotNull(productInventory, "商品库存不存在");
        CopyUtils.copyProperties(productInventoryDto, productInventory);
        productInventory.setUpdateDate(new Date());
        productInventoryMapper.updateById(productInventory);
    }

    @Override
    public void delete(Long inventoryId) {
        AssertUtil.isNotNull(inventoryId, "商品库存ID不能为空");
        productInventoryMapper.deleteById(inventoryId);
    }

    @Override
    public void deleteBatch(List<Long> inventoryIds) {
        AssertUtil.isNotNull(inventoryIds, "商品库存ID列表不能为空");
        productInventoryMapper.deleteBatchIds(inventoryIds);
    }
}
